﻿using System;
using System.Collections.Generic;
using System.Data;
using System.Data.SqlClient;
using System.Text;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using OTS.BLL;

public partial class OLPC_MCApproval : System.Web.UI.Page
{
    protected void Page_Load(object sender, EventArgs e)
    {
        if (!IsPostBack)
        {
            CommonMethod.IsTimeOutUserCode(this, CommonModel.errorTimeOutUrl1);
            Session["assigneddt"] = null;
            Session["dtlist"] = null;
            InitUi();
            tc.ActiveTabIndex = 0;
        }
    }
    // 页面权限
    private void SettingRight()
    {
        bool[] rightlist = SysRightBLL.GetRightByType(new string[] { 
            Session["UserCode"].ToString(), 
            Session["CoCode"].ToString(), 
            "mctoinvoice"});
        if (!rightlist[0])
        { this.Page.Response.Write(String.Format("<script language='javascript'>window.parent.location.href='{0}';</script>", CommonModel.loginOut1)); }

        //tc.TabPages[1].Visible = rightlist[1];
        //gvimported.Columns[1].Visible = rightlist[3];
        //gvimported.Columns[0].Visible = rightlist[2];
        //tc.TabPages[2].Visible = rightlist[1] ? true : (rightlist[3] ? true : false);
    }

    // 初始化绑定
    private void InitUi()
    {
        InitAssignedDataBind();
    }
    // 已匹配收款数据
    private void InitAssignedDataBind()
    {
        DataTable tempdt = new DataTable();
        if (Session["assigneddt"] != null)
        {
            tempdt = Session["assigneddt"] as DataTable;
        }
        else
            tempdt = MCApprovalBLL.GetList(" and len(ContractNoMain)>0 and OAppliedAmount>0 ");//
        gvAssigned.DataSource = tempdt;
        gvAssigned.DataBind();
        Session["assigneddt"] = tempdt;
    }

    protected void gvAssigned_AfterPerformCallback(object sender,
        DevExpress.Web.ASPxGridView.ASPxGridViewAfterPerformCallbackEventArgs e)
    {
        InitAssignedDataBind();
    }

    // 匹配行事件
    protected void gvAssigned_RowCommand(object sender, 
        DevExpress.Web.ASPxGridView.ASPxGridViewRowCommandEventArgs e)
    {
        try
        {
            string temp = e.CommandArgs.CommandArgument.ToString();
            int Inttemp = int.Parse(temp);
            string cmd = e.CommandArgs.CommandName;
            if (cmd == "assigning")
            {
                #region

                int rowindex = e.VisibleIndex;
                DataRow row = gvAssigned.GetRow(rowindex) as DataRow;
                lblContractNoMain.Text = row["ContractNoMain"].ToString();
                lblOrderNo.Text = row["TradingSoNo"].ToString();
                string strWhere = string.Format(" ContractNO='{0}' and SalesOrderNo='{1}' ",
                    row["ContractNoMain"], row["TradingSoNo"]);

                DataTable tempdt = MCApprovalBLL.GetInvoice(lblContractNoMain.Text, lblOrderNo.Text);
                ddlInvoice.DataSource = tempdt;
                ddlInvoice.ValueField = "Amount";
                ddlInvoice.TextField = "Invoice";
                if (ddlInvoice.Items.Count > 0)
                {
                    ddlInvoice.SelectedIndex = 0;
                    lblInvoiceAmount.Text = ddlInvoice.Items[0].Value.ToString();
                }

                DataTable tdmpdt2 = AccountStatementManualBLL.GetList(strWhere);
                gv.DataSource = tdmpdt2;
                gv.DataBind();


                tc.ActiveTabIndex = 1;



                #endregion
            }
        }
        catch (Exception ex)
        {
            Log.WritLog("gvAssigned_RowCommand:", ex.ToString());
        }
    }


    protected void btnInvoiceShow_Click(object sender, EventArgs e)
    {
        Session["dtlist"] = null;
        InvoiceInitDataBind();
    }
    // 已匹配发票绑定
    private void InvoiceInitDataBind()
    {
        DataTable tempdt = new DataTable();
        if (Session["dtlist"] != null)
        {
            tempdt = Session["dtlist"] as DataTable;
        }
        else
        {
            tempdt = MCApprovalBLL.GetInvoic("");
        }
        gv.DataSource = tempdt;
        gv.DataBind();
        Session["dtlist"] = tempdt;
    }
    protected void gv_AfterPerformCallback(object sender, DevExpress.Web.ASPxGridView.ASPxGridViewAfterPerformCallbackEventArgs e)
    {
        InvoiceInitDataBind();
    }
    protected void gv_RowCommand(object sender, DevExpress.Web.ASPxGridView.ASPxGridViewRowCommandEventArgs e)
    {
        string ss = "sfsdf";
    }
    protected void ddlInvoice_SelectedIndexChanged(object sender, EventArgs e)
    {
        lblInvoiceAmount.Text = ddlInvoice.Items[ddlInvoice.SelectedIndex].Value.ToString();
    }
    public void GetSelectedAmount()
    {
        if (CommonMethod.CheckNUM(lblInvoiceAmount.Text))
        {
            double invoiceamount = double.Parse(lblInvoiceAmount.Text);
            double result = 0;
            List<object> totals = gv.GetSelectedFieldValues("ApplyAmount");
            for (int i = 0; i < totals.Count; i++)
            {
                if (totals[i] == null || totals[i] == DBNull.Value) continue;
                result += (double)totals[i];
            }
            if (invoiceamount < result)
            {
                CommonMethod.regAlertMessage(Upd, "已选金额不能大于发票金额!", this);
            }
        }
        else
            CommonMethod.regAlertMessage(Upd, "发票金额不对！", this);
    }
    protected void btnSure_Click(object sender, EventArgs e)
    {
        double invoiceamount = double.Parse(lblInvoiceAmount.Text);
        double result = 0;
        List<object> totals = gv.GetSelectedFieldValues("ApplyAmount");
        for (int i = 0; i < totals.Count; i++)
        {
            if (totals[i] == null || totals[i] == DBNull.Value) continue;
            result += (double)totals[i];
        }
        if (invoiceamount >= result)
        {
            List<object> listid = gv.GetSelectedFieldValues("AccountStatementManualID");
            if (listid != null && listid.Count > 0)
            {
                result = MCApprovalBLL.CreitCheck(listid, ddlInvoice.Text);
                if (result < 1)
                    CommonMethod.regAlertMessage(Upd, "执行失败！", this);
                else
                    gv.DataSource = null;
            }
        }
        else
        {
            CommonMethod.regAlertMessage(Upd, "已选金额不能大于发票金额!", this);
        }
    }
}
